package com.wythe.mall.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hb.dialog.myDialog.MyAlertDialog;
import com.wythe.mall.R;
import com.wythe.mall.adapter.AddressListAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.AddressBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;
import com.wythe.mall.utils.ToastUtil;
import com.wythe.mall.utils.UserManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AddressActivity extends BaseActivity implements View.OnClickListener, AddressListAdapter.OnItemClickListener, AddressListAdapter.OnViewClickListener {

    private RecyclerView mRecyclerView;
    private AddressListAdapter mAdapter;
    private TextView mRightButton;

    private long exitTimeLong;
    private ArrayList<AddressBean> mainArrayList;
    private Bundle mBundle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mBundle = getIntent().getExtras();
        if (mainArrayList == null) {
            mainArrayList = new ArrayList<>();
        }
        mainArrayList.clear();
        initView();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRightButton = (TextView) findViewById(R.id.title_right_button);
        mRightButton.setVisibility(View.VISIBLE);
        mRightButton.setOnClickListener(this);
        ((TextView) findViewById(R.id.titleText)).setText("地址管理");
        mRecyclerView = (RecyclerView) findViewById(R.id.mainPullRefreshView);
        mAdapter = new AddressListAdapter(mainArrayList);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnViewClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);//设置布局管理器
        mRecyclerView.setAdapter(mAdapter);
    }

    public void initData() {
        exitTimeLong = 0L;
    }

    @Override
    public void onResume() {
        super.onResume();
        getAddress();
    }

    //自定义方法

    private void getAddress() {
        if (!UserManager.getInstance().isLogin()) {
//            GotoActivity.gotoActiviy(this.getActivity(), LoginActivity.class);
            ToastUtil.show(this,"请先登录！");
            SharePreferenceUtils.removeString(this.getActivity(), "KEY_TOKEN");
            return;
        }
        JSONObject param = new JSONObject();
        ApiOkHttp.getAddressList(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    mainArrayList.clear();
                    JSONArray data1 = data.getJSONArray("data");
                    for (int i = 0; i < data1.length(); i++) {
                        AddressBean bean = new Gson().fromJson(data1.getJSONObject(i).toString(), AddressBean.class);
                        mainArrayList.add(bean);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    private void delAddress(int addressId) {
        JSONObject param = new JSONObject();
        try {
            param.put("id",addressId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.deleteAddress(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    mainArrayList.clear();
                    JSONArray data1 = data.getJSONArray("data");
                    for (int i = 0; i < data1.length(); i++) {
                        AddressBean bean = new Gson().fromJson(data1.getJSONObject(i).toString(), AddressBean.class);
                        mainArrayList.add(bean);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.title_right_button:
                GotoActivity.gotoActiviy(this,AddressAddActivity.class);
                break;
        }
    }

    @Override
    public void onEdit(int position, AddressBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("address",bean);
        GotoActivity.gotoActiviy(this,AddressAddActivity.class,bundle);
    }

    @Override
    public void onDelete(int position, final AddressBean bean) {
        MyAlertDialog myAlertDialog = new MyAlertDialog(this);
        myAlertDialog.builder();
        myAlertDialog.setTitle("温馨提示");
        myAlertDialog.setMsg("你确定将此收货地址删除？");
        myAlertDialog.setPositiveButton("确认", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delAddress(bean.getId());
            }
        });
        myAlertDialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        myAlertDialog.show();
    }

    @Override
    public void onClick(int position, AddressBean bean) {
        if (mBundle == null) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("address", bean);
            intent.putExtras(bundle);
            AddressActivity.this.setResult(Activity.RESULT_OK, intent);
            finish();
        }
    }
}
